package hongniang

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
	"time"
)

type BankinfoBody struct {
	Hongniangid int64   `json:"hongniangid" gorm:"hongniangid"`
	Khh         string  `json:"khh" gorm:"khh"`
	Bankid      string  `json:"bankid" gorm:"bankid"`
	Name        string  `json:"name" gorm:"name"`
	Phone       string  `json:"phone" gorm:"phone"`
	Price       float64 `json:"price" gorm:"price"`
}

func Bankinfo(ctx *gin.Context) {
	request := BankinfoBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	var bank models.Bank
	data_source.Db.Where("hongniangid = ?", hongniangid).First(&bank)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    bank,
		"msg":     "ok",
	})
	return
}
func Bankupdate(ctx *gin.Context) {
	request := BankinfoBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	khh := request.Khh
	bankid := request.Bankid
	name := request.Name
	phone := request.Phone
	var bank models.Bank
	data_source.Db.Where("hongniangid = ?", hongniangid).First(&bank)
	if bank.ID == 0 {
		bankadd := models.Bank{
			Hongniangid: hongniangid,
			Khh:         khh,
			Bankid:      bankid,
			Name:        name,
			Phone:       phone,
		}
		data_source.Db.Create(&bankadd)
	} else {
		bankadd := models.Bank{
			Khh:    khh,
			Bankid: bankid,
			Name:   name,
			Phone:  phone,
		}
		data_source.Db.Where("hongniangid = ?", hongniangid).Updates(&bankadd)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type TixianaddBody struct {
	Hongniangid int64   `json:"hongniangid" gorm:"hongniangid"`
	Yhkid       int64   `json:"yhkid" gorm:"yhkid"`
	Price       float64 `json:"price" gorm:"price"`
	PageNo      int     `json:"pageNo"`
	Pagesize    int     `json:"pagesize"`
	Status      int64   `json:"status" gorm:"status"` // 1进行中 2成功 3拒绝
	State       int64   `json:"state" gorm:"state"`   // 1加 2减
}
type HongliangpricelistRBody struct {
	Data       []models.Hongniangprice `json:"data"`
	PageNo     int                     `json:"pageNo"`
	PageSize   int                     `json:"pageSize"`
	TotalCount int64                   `json:"totalCount"`
	TotalPage  int64                   `json:"totalPage"`
}

func Hongliangpricelist(ctx *gin.Context) {
	request := TixianaddBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	state := request.State
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Hongniangprice
	res := data_source.Db.Where("hongniangid = ?", hongniangid)
	if state > 0 {
		res = res.Where("state = ?", state)
	}
	res.Model(&models.Hongniangprice{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := HongliangpricelistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type TixianlistRBody struct {
	Data       []models.Hongniangtixian `json:"data"`
	PageNo     int                      `json:"pageNo"`
	PageSize   int                      `json:"pageSize"`
	TotalCount int64                    `json:"totalCount"`
	TotalPage  int64                    `json:"totalPage"`
}

func Tixianlist(ctx *gin.Context) {
	request := TixianaddBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid
	status := request.Status
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Hongniangtixian
	res := data_source.Db.Where("hongniangid = ?", hongniangid)
	if status > 0 {
		res = res.Where("status = ?", status)
	}
	res.Model(&models.Hongniangtixian{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := TixianlistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}
func Tixianadd(ctx *gin.Context) {
	request := TixianaddBody{}
	ctx.ShouldBind(&request)
	hongniangid := request.Hongniangid

	price := request.Price
	var bank models.Bank
	data_source.Db.Where("hongniangid = ?", hongniangid).First(&bank)
	var hongniang models.Hongniang
	data_source.Db.Where("id = ?", hongniangid).First(&hongniang)
	if hongniang.Price < price {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "提现金额超出余额",
		})
		return
	}
	tixian := models.Hongniangtixian{
		Hongniangid: hongniangid,
		Yhkid:       bank.ID,
		Price:       price,
		Addtime:     time.Now().Unix(),
		Status:      1,
	}
	data_source.Db.Create(&tixian)

	hnpirce := models.Hongniangprice{
		Hongniangid: hongniangid,
		State:       2,
		Addtime:     time.Now().Unix(),
		Title:       "红娘提现",
		Price:       price,
		Houprice:    hongniang.Price + price,
	}
	data_source.Db.Create(&hnpirce)
	hnupdate := models.Hongniang{
		Price:            hongniang.Price - price,
		Tixianzhongprice: hongniang.Tixianzhongprice + price,
	}
	data_source.Db.Where("id = ?", hongniangid).Updates(&hnupdate)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
